// dropdownUtils.js
/**
 * 通用下拉选择事件处理函数
 * @param {Object} item - 选择的下拉项
 * @param {Number} labelIndex - 当前选择的下拉框编号
 * @param {Object} context - 包含 selectedLabel1, selectedLabel2, selectedLabel3 和 store 的上下文
 */
export const handleDropdownSelectFn = (item, labelIndex, context) => {
  const {
    selectedLabel1,
    selectedLabel2,
    selectedLabel3,
    store,
    getLableName,
    getLableCode,
    storeInfo,
    isMajor,
  } = context;

  // 根据 labelIndex 动态赋值对应的 selectedLabel
  if (labelIndex == 1) {
    selectedLabel1.value = getLableName[item.id];
  } else if (labelIndex == 2) {
    selectedLabel2.value = getLableName[item.id];
  } else if (labelIndex == 3) {
    selectedLabel3.value = getLableName[item.id];
  }

  // 更新 storeInfo
  store.dispatch(storeInfo.value, [
    getLableCode[selectedLabel1.value],
    getLableCode[selectedLabel2.value],
    getLableCode[selectedLabel3.value],
  ]);

  // 更新状态
  store.dispatch('znxk/updateManualSelect', { isMajor, flag: true });
  store.dispatch('znxk/updateTableSelect', { isMajor, flag: false });
};

/**
 * 检查选项是否应当被禁用
 * @param {String} itemSubName - 选项的 subName 属性
 * @param {Number} currentLabelIndex - 当前下拉框编号
 * @param {Number} xkType - xkType 的值
 * @param {Object} context - 包含 selectedLabel1, selectedLabel2 和 selectedLabel3 的上下文
 * @returns {Boolean} 是否禁用
 */
export const isOptionDisabledFn = (
  itemSubName,
  currentLabelIndex,
  xkType,
  context,
) => {
  const { selectedLabel1, selectedLabel2, selectedLabel3 } = context;

  if (xkType == 3) {
    return false;
  }

  // 根据当前下拉框的索引，判断是否禁用其他选项
  if (currentLabelIndex == 1) {
    return (
      itemSubName == selectedLabel2.value || itemSubName == selectedLabel3.value
    );
  } else if (currentLabelIndex == 2) {
    return (
      itemSubName == selectedLabel1.value || itemSubName == selectedLabel3.value
    );
  } else if (currentLabelIndex == 3) {
    return (
      itemSubName == selectedLabel1.value || itemSubName == selectedLabel2.value
    );
  }
  return false;
};
